package com.webull.information.center.carwler.service;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.webull.framework.mvc.ApiException;
import com.webull.information.center.carwler.common.model.BaseNewsRel;
import com.webull.information.center.carwler.common.model.BranchNews;
import com.webull.information.center.carwler.common.util.BranchNewsUtil;
import com.webull.information.center.carwler.common.util.CarwlerJobStateUtil;
import com.webull.information.center.carwler.dao.CarwlerNewsDao;
import com.webull.information.center.dao.InfoNewsDao;
import com.webull.information.center.dao.NewsAssociateDao;
import com.webull.information.center.validator.NewsValidator;
import com.webull.information.client.model.News;
import com.webull.information.client.model.NewsAssociate;
import com.webull.information.client.model.parm.NewsRelType;

/**
 * 爬取程序写入数据库
 * Created by smj on 2017/5/16.
 */
@Component
public class CarwlerNewsService {
    protected final Logger logger = LogManager.getLogger(getClass());
    private @Autowired
    CarwlerJobStateUtil carwlerJobStateUtil;
    private @Autowired
    CarwlerNewsDao carwlerNewsDao;
    private @Autowired
    InfoNewsDao infoNewsDao;
    private @Autowired
    NewsAssociateDao newsAssociateDao;
    private @Autowired
    NewsValidator newsValidator;

    /**
     * 增加base的关联关系
     *
     * @param targetId
     * @param type
     * @param newsId
     * @throws ApiException
     */
    public void addExistNewsInfoRel(Integer targetId, NewsRelType type, Integer newsId) throws ApiException {
        newsValidator.validateGetById(targetId);
        newsValidator.validateGetById(newsId);
        BaseNewsRel rel = new BaseNewsRel();
        rel.setNewsId(newsId);
        rel.setTargetId(targetId);
        rel.setTargetType(BranchNewsUtil.genBranchTargetType(type));
        if (NewsRelType.currency.equals(type)) {
            if (infoNewsDao.existCurrencyNewsRel(targetId, newsId)) return;
        } else if (NewsRelType.futureCategory.equals(type)) {
            if (infoNewsDao.existFutureCatNewsRel(targetId, newsId)) return;
        } else if (NewsRelType.ticker.equals(type)) {
            if (infoNewsDao.existTickerNewsRel(targetId, newsId)) return;
        } else if (NewsRelType.associate.equals(type)) {
            NewsAssociate assoc = new NewsAssociate();
            assoc.setNewsId(targetId);
            assoc.setAssNewsId(newsId);
            if (newsAssociateDao.existNewsAssociate(assoc)) return;
        }
        if (rel.getTargetType() != null)
            carwlerNewsDao.addExistNewsRel(carwlerJobStateUtil.getBaseRelTable(), rel);
    }


    public Long addBranchNewsRecord(Integer targetId, String targetMd5, NewsRelType type, News news) throws ApiException {
        BranchNews branchNews = new BranchNews();
        BeanUtils.copyProperties(news, branchNews);
        return addBranchNewsRecord2(targetId, targetMd5, type, branchNews);
    }

    public Long addBranchNewsRecord2(Integer targetId, String asscMd5, NewsRelType type, BranchNews branchNews) throws ApiException {
		if (!NewsRelType.homePage.equals(type) && !NewsRelType.associate.equals(type)) {
            newsValidator.validateGetById(targetId);
        }
        branchNews.setId(null);
        branchNews.setTargetId(targetId);
        branchNews.setTargetType(BranchNewsUtil.genBranchTargetType(type));
        branchNews.setTargetMd5(asscMd5);
        Long branchId = Long.valueOf(carwlerNewsDao.addBranchNews(carwlerJobStateUtil.getEdenNewsTable(), branchNews));
        return branchId;
    }


    public List<BranchNews> searchBranchNewsByMd5(String urlMd5) {
        return carwlerNewsDao.searchBranchNewsByMd5(carwlerJobStateUtil.getEdenNewsTable(), urlMd5);
    }

	public List<BranchNews> searchBranchNewsBySumMd5(String sumMd5) {
		return carwlerNewsDao.searchBranchNewsBySumMd5(carwlerJobStateUtil.getEdenNewsTable(), sumMd5);
	}

}
